﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinformText0001
{
    public partial class EditForm : Form
    {
        public EditForm()
        {
            InitializeComponent();
        }
        public EditForm(int id, string title, string author )
        {
            InitializeComponent();

            this.id = id;
            txtAuthor.Text = title;
            txtTitle.Text = author;
          
        }

        private int id;

        private void button1_Click(object sender, EventArgs e)
        {
           

           
            var Author = txtAuthor.Text;
             var Title = txtTitle.Text;
 
         
            //更新
            if (id > 0)
            {
                if (!string.IsNullOrEmpty(Author))
                {
                    var sql = string.Format("update Articles set Author='{0}',Title='{1}' where Id={2}", Author,Title, this.id);
                    var res = DbHelper.AddOrUpDateOrDelete(sql);

                    if (res == 1)
                    {
                        MessageBox.Show("修改成功！", "提示");
                        this.DialogResult = DialogResult.Yes;
                        this.Close();
                    }
                }
            }
            //新增
            else
            {
                if (!string.IsNullOrEmpty(Author))
                {
                    var sql = string.Format("insert into Articles(Author, Title) values('{0}', '{1}')", Author,Title);

                    var res = DbHelper.AddOrUpDateOrDelete(sql);

                    if (res == 1)
                    {
                        MessageBox.Show("添加成功！", "提示");
                        this.DialogResult = DialogResult.Yes;
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("添加失败！", "提示");
                        this.Close();
                    }

                }
                else
                {
                    MessageBox.Show("不能为空！", "提示");
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
             this.DialogResult = DialogResult.No;
            this.Close();
        }

        private void txtAuthor_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
